<?php
namespace app\common\service\message\dealer;
use app\common\service\message\Basics;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\model\Setting as SettingModel;
/**
 *  当面付交易提醒
 * Class Notice
 * @package app\common\service\message\face
 */
class ProfitNotice extends Basics
{

    /**
     * 参数列表
     * @var array
     */
    protected $param = [
        'order' => [],
        'to_user_id'=>'',
        'money'=>'',
        'order_type' =>'',
    ];

    /**
     * 订单页面链接
     * @var array
     */
    private $pageUrl = [
        OrderTypeEnum::MASTER  => 'pages/order/detail',//普通订单
        OrderTypeEnum::SHARING => 'pages/sharing/order/detail/detail',//拼团
        OrderTypeEnum::FACEPAY => 'pages/face/index',//当面付
        OrderTypeEnum::GIFTSPAY=> 'pages/gift-bag/index',//爆品礼
        OrderTypeEnum::VIPCARD => 'pages/vip/vip',//爆品礼
    ];

    /**
     * 发送消息通知
     * @param array $param
     * @return mixed|void
     * @throws \think\Exception
     */
    public function send($param)
    {
        // 记录参数
        $this->param = $param;
        // 微信订阅消息通知用户
        $this->onSendWxSubMsg();
    }

    /**
     * 微信订阅消息通知用户
     * @return bool|mixed
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    private function onSendWxSubMsg()
    {
        $orderInfo = $this->param['order'];
        $wxappId = $orderInfo['wxapp_id'];
        $orderType = $this->param['order_type'];
        $money=$this->param['money'];
        // 获取订阅消息配置
        $template = SettingModel::getItem('submsg', $wxappId)['dealer']['profit_notice'];
        if (empty($template['template_id'])) {
            return false;
        }
        //获取上级用户
//        $ParentUser=$this->getParentUser($orderInfo);
//        if (empty($ParentUser)){
//            return  false;
//        }
        // 发送订阅消息
        return $this->sendWxSubMsg($wxappId,
            [
            'touser' => $this->getUserOpneId($this->param['to_user_id']),//上级用户openId
            'template_id' => $template['template_id'], //模板id
            //'page' => "pages/dealer/order/order",//详情地址
            'page' => "{$this->pageUrl[$orderType]}?order_id={$orderInfo['order_id']}",
            'data' => [
                //会员名
                $template['keywords'][0] => ['value' => $orderInfo['user']['nickName']],
                //订单金额
                $template['keywords'][1] => ['value' => $orderInfo['pay_price']],
                //我的收益
                $template['keywords'][2] => ['value' => $money],
                // 下单时间
                $template['keywords'][3] => ['value' => format_time($orderInfo['pay_time'])],
            ]
        ]);
    }

    /**
     * 获取下单用户名称
     * @param $userId
     * @return string
     */
    private function getUserName($userId)
    {
        return db('user')->where('user_id',$userId)->value('nickName');
    }

    /**
     * 获取分销订单
     * @param $order
     * @return array|bool|false|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getParentUser($order){
        //查找下单用户上级
        $parent=db('dealer_order')->where(['wxapp_id'=>$order['wxapp_id'],'user_id'=>$order['user_id'],'order_id'=>$order['order_id']])->find();
        if (!empty($parent)){
            return false;
        }
        return  $parent;
    }

    /**
     *  获取上级用户的openid
     * @param $userId
     * @return bool|float|mixed|string|null
     */
    private function getUserOpneId($userId){
        return db('user')->where('user_id',$userId)->value('open_id');
    }


}